#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    string reverseWords(string s) {
        int left, right = -1;
        int end = s.rfind(' ', s.npos);
        int n = s.size() - end;
        while (left != s.size())
        {
            left = right;
            right = s.find(' ', left + 1);
            if (right > s.size())
                right = s.size();
            reverse(s.begin() + left + 1, s.begin() + min(right, (int)s.size()));
        }
        return s;
    }
};